<?php

	/**
	 * productName 仓库名称;
	 * auther QiuXiangCheng;
	 * date 2016/07/06
	 */

	class Warehouse_IndexController extends Controller_Base{

		public function init() {

			parent::init();
			$this -> warehouseModel = new Warehouse_Model_Warehouse();
		}

		public function indexAction(){

			$this -> smarty -> assign('conf', Helper_Lang::$conf);
			$this -> smarty -> assign('list', $this -> warehouseModel -> getWarehouse()) -> display('index.php');
		}

		public function listexameAction(){

			Helper_Array::du($this -> _request -> getPost());
		}

		/**
		 * 增加仓库信息
		 * @param $_POST
		 * @return Json
		 */
		public function addwarehouseAction(){

			$result = ['status' => 0, 'message' => Helper_Lang::$conf['Err1003']];
			if($this -> _request -> isPost()){
				$data = $this -> _request -> getPost();
				// 验证表单
				/*if($this -> warehouseModel -> checkFormAdd($data)){
					$result['message'] = Helper_Lang::$conf['Err1002'];
					$this -> _helper -> json($result);
				}*/
				$examineData = [
					'leader' => $data['leader'], 
					'director' => $data['director'], 
					'manager' => $data['manager'],
				];
				// sleep(1);
				unset($data['manager'], $data['director'], $data['leader']);
				if($lastAddId = $this -> warehouseModel -> addWarehouse($data)){
					$canUpdate = 0;
					$sql = 'INSERT INTO c_warehouse_ex(auditor, quota, maxnum, warehouse_id) VALUES';
					foreach($examineData as $v){
						if(!isset($v['auditor']) || empty($v['auditor']) || empty($v['quota']) || empty($v['maxnum'])){
							continue;
						}
						$canUpdate ++;
						$v['warehouse_id'] = $lastAddId;
						$sql .= '(' . implode(', ', $v) . '),';
					}
					if($canUpdate){
						if(Model_Db::getInstance() -> query(rtrim($sql, ','))){
							$result['status'] = 1;
							$result['message'] = Helper_Lang::$conf['Suc1001'];
						}
					}
				}
				$this -> _helper -> json($result);
			}
			$this -> smarty -> display('add.php');
		}

		public function examineAction(){

			$post = $this -> _request -> getPost();
// 			Helper_Array::pr($post);
			die;
		}
	}